import json
import time

import requests,re
def download_img(url,right,left,path):
    """
    下载图片到本地
    :param url: 页面url
    :param right: 图片地址右边界
    :param left: 图片地址左边界
    :param path: 图片保存在本地的路径
    :return:
    """
    # url = 'https://www.dgtle.com/article-1535229-1.html'
    r = requests.get(url)
    # r.text，获取响应的文本信息，以字符串返回
    # r.content，获取响应的文本信息，以字节串返回
    # r.json()，获取响应的文本信息，以json格式返回
    page = r.text  # 获取页面源码  把响应的二进制转换为字符串
    img_url = re.findall(f'{left}(.*?){right}', page)  # 获取所有图片的url
    print(f'开始下载，总共{len(img_url)}张图片')
    for i,j in enumerate(img_url):
        print(f'正在下载第{i + 1}/{len(img_url)}张图片')
        with open(f'{path}/{i + 1}.png', 'wb') as f:
            r = requests.get(url=j)  # 访问图片地址
            f.write(r.content)

def woniusales(url,data):
    # r = requests.get(url)
    r = requests.post(url,data)
    page = r.text
    send_md = r.request.method
    send_url = r.request.url
    send_header = r.request.headers
    send_body = r.request.body
    print(send_md, send_url, send_header, send_body, sep='\n')

    recv_status_code = r.status_code
    print("收到的状态码", recv_status_code)

    recv_header = r.headers
    print("收到的header", recv_header)

    # recv_content = r.content
    # print("收到的body二进制", recv_content)

    # recv_text = r.text  # 把响应的body二进制转为字符串。
    # print("收到的文本: ", recv_text)

    print('响应头：',r.headers)
    print('cookies：',r.cookies)
    response_text = r.text
    if response_text=="login-pass":
        print('登录成功')
    else:
        print('登录失败')



# if __name__ == '__main__':
    # url = 'https://www.dgtle.com/article-1535229-1.html'
    # right = '" class'
    # left = 'data-original="'
    # path = r'C:\Users\Administrator\Downloads\下载图片'
    # download_img(url,right,left,path)

    # url = 'http://localhost:8080/woniusales/user/login'
    # data = {"username":"admin","password":"admin123","verifycode":"0000"}
    # woniusales(url,data)

    # url = "http://localhost:8080/woniusales/"
    # r = requests.get(url)
    # html = r.content  # 拿二进制
    # with open("woniusales.html", "wb") as f:
    #     f.write(html)
    #
    # url2="http://localhost:8080/woniusales/image/background.jpg"
    # r2=requests.get(url2)
    # # print(r2.text)不要用text获取图片数据，解码可能会出错的。
    # with open("background.jpg", "wb") as f2:
    #     f2.write(r2.content)

# url = 'http://localhost:8080/woniusales/customer/search'
# data={"customerphone":"","page":"1"}
# r=requests.post(url, data)
#
# response_json=r.json()#返回的结果必须是json格式，才
# # print(type(response_json),response_json)
# print([i["customername"] for i in response_json])
#
# # print(type(json.loads(r.text)),json.loads(r.text))
# print(json.loads(r.text))


# url="http://localhost:8080/woniusales/customer/add"
# userinfo = {"customername":"xinyi","customerphone":"1581111124","childsex":"男","childdate":"2021-03-18","creditkids":"4","creditcloth":"4"}
# s = requests.session()
# print(s.cookies)
#
# login = s.post('http://localhost:8080/woniusales/user/login',{"username":"admin","password":"admin123","verifycode":"0000"})
# print(login.text)
# print(s.cookies)
#
# add = s.post(url,userinfo)
# print(add.text)
# print(add.request.headers)



# s=requests.session()
# s.post("http://localhost:8080/woniusales/user/login",{"username":"admin","password":"admin123","verifycode":"0000"})

# upload_url="http://localhost:8080/woniusales/goods/upload"
# data={"batchname":"GB2021031712"}  # 批次名
# f = {"batchfile":("testwoniusales.xls",open(r"E:\testwoniusales.xls","rb"),"application/vnd.ms-exce1")}  # 完全和web发起的请求一致了
# upload=s.post(upload_url, data, files=f)
# print(upload.text)

# logout_url = "http://localhost:8080/woniusales/user/logout"
# logout = s.get(logout_url,allow_redirects=False)
# print(s.cookies, logout.request.url, logout.status_code, logout.headers, sep='\n')



# 操作https
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)  # 忽略警告


r=requests.get('https://www.baidu.com',verify=False)
print(r.text)

















